package com.whc.gulimall.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whc.common.to.SkuReductionTo;
import com.whc.common.utils.PageUtils;
import com.whc.common.utils.Query;
import com.whc.gulimall.coupon.dao.SkuFullReductionDao;
import com.whc.gulimall.coupon.entity.MemberPriceEntity;
import com.whc.gulimall.coupon.entity.SkuFullReductionEntity;
import com.whc.gulimall.coupon.entity.SkuLadderEntity;
import com.whc.gulimall.coupon.service.MemberPriceService;
import com.whc.gulimall.coupon.service.SkuFullReductionService;
import com.whc.gulimall.coupon.service.SkuLadderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author angaoen
 */
@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    /**
     * 商品阶梯价格 Service
     */
    @Resource
    private SkuLadderService skuLadderService;

    /**
     * 商品会员价格 Service
     */
    @Resource
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存sku满减，优惠等信息
     *
     * @param skuReductionTo 传输对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        // 保存阶梯价格
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtil.copyProperties(skuReductionTo, skuLadderEntity);
        if (skuLadderEntity.getFullCount() > 0) {
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }

        // 保存满减信息
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtil.copyProperties(skuReductionTo, skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice().compareTo(BigDecimal.ZERO) > 0) {
            save(skuFullReductionEntity);
        }

        List<MemberPriceEntity> memberPriceEntities = skuReductionTo.getMemberPrice().stream()
                .filter(item -> item.getPrice().compareTo(BigDecimal.ZERO) > 0)
                .map(item -> {
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                    memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                    memberPriceEntity.setMemberLevelId(item.getId());
                    memberPriceEntity.setMemberLevelName(item.getName());
                    memberPriceEntity.setMemberPrice(item.getPrice());
                    memberPriceEntity.setAddOther(1);
                    return memberPriceEntity;
                }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }

}
